package com.feinno.urcs.group.http.util;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wuzhiguo on 15-5-12.
 */
public class XcapParser {

    private static final String NODE_SELECTOR_SPLIT = "/~~";
    private static final Map<String, String> transCharsMap;

    static {
        transCharsMap = new HashMap<>();
        transCharsMap.put("%5b", "[");
        transCharsMap.put("%5d", "]");
        transCharsMap.put("%22", "\"");
    }

    private String xcapUri;
    private String documentSelector;
    private String nodeSelector;
    private String auid;
    private String zone;
    private String xui;
    private String filename;
    private String[] documentSelectorArray;
    private String[] nodeSelectorArray;

    public XcapParser() {
        this.xcapUri = "";
        this.documentSelector = "";
        this.nodeSelector = "";
        this.auid = "";
        this.zone = "";
        this.xui = "";
        this.filename = "";
        this.documentSelectorArray = null;
        this.nodeSelectorArray = null;
    }

    public void parse(String xcapUri) {
        if (xcapUri != null) {
            this.xcapUri = xcapUri;
            int nodeSelectorSplitIndex = xcapUri.indexOf(NODE_SELECTOR_SPLIT);
            if (nodeSelectorSplitIndex >= 0) {
                this.documentSelector = xcapUri.substring(0, nodeSelectorSplitIndex);
                this.nodeSelector = xcapUri.substring(nodeSelectorSplitIndex + NODE_SELECTOR_SPLIT.length());
                this.nodeSelector = transNodeSelector(this.nodeSelector);
            } else {
                this.documentSelector = xcapUri;
            }

            if (this.documentSelector != null && this.documentSelector.length() > 0) {
                this.documentSelectorArray = this.documentSelector.split("/");
            }

            if (this.nodeSelector != null && this.nodeSelector.length() > 0) {
                this.nodeSelectorArray = this.nodeSelector.split("/");
            }

            if (this.documentSelectorArray != null) {
                if (this.documentSelectorArray.length > 1) {
                    this.auid = this.documentSelectorArray[1];
                }
                if (this.documentSelectorArray.length > 2) {
                    this.zone = this.documentSelectorArray[2];
                }
                if (this.documentSelectorArray.length > 3) {
                    this.xui = this.documentSelectorArray[3];
                }
                if (this.documentSelectorArray.length > 4) {
                    for (int i = 4; i < this.documentSelectorArray.length; i++) {
                        this.filename += this.documentSelectorArray[i] + "/";
                    }

                    this.filename = this.filename.substring(0, this.filename.length() - 1);
                }
            }
        }
    }

    public String transNodeSelector(String nodeSelector) {
        if (nodeSelector != null && transCharsMap != null) {
            for (String key : transCharsMap.keySet()) {
                nodeSelector = nodeSelector.replace(key, transCharsMap.get(key));
            }
        }

        return nodeSelector;
    }

    public String getXcapUri() {
        return xcapUri;
    }

    public void setXcapUri(String xcapUri) {
        this.xcapUri = xcapUri;
    }

    public String getDocumentSelector() {
        return documentSelector;
    }

    public void setDocumentSelector(String documentSelector) {
        this.documentSelector = documentSelector;
    }

    public String getNodeSelector() {
        return nodeSelector;
    }

    public void setNodeSelector(String nodeSelector) {
        this.nodeSelector = nodeSelector;
    }

    public String getAuid() {
        return auid;
    }

    public void setAuid(String auid) {
        this.auid = auid;
    }

    public String getZone() {
        return zone;
    }

    public void setZone(String zone) {
        this.zone = zone;
    }

    public String getXui() {
        return xui;
    }

    public void setXui(String xui) {
        this.xui = xui;
    }

    public String getFilename() {
        return filename;
    }

    public void setFilename(String filename) {
        this.filename = filename;
    }

    public String[] getDocumentSelectorArray() {
        return documentSelectorArray;
    }

    public void setDocumentSelectorArray(String[] documentSelectorArray) {
        this.documentSelectorArray = documentSelectorArray;
    }

    public String[] getNodeSelectorArray() {
        return nodeSelectorArray;
    }

    public void setNodeSelectorArray(String[] nodeSelectorArray) {
        this.nodeSelectorArray = nodeSelectorArray;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(String.format("[XcapParser]\n"));
        sb.append(String.format("xcapUri:%s\n", xcapUri));
        sb.append(String.format("documentSelector:%s\n", documentSelector));
        sb.append(String.format("nodeSelector:%s\n", nodeSelector));
        sb.append(String.format("auid:%s\n", auid));
        sb.append(String.format("zone:%s\n", zone));
        sb.append(String.format("xui:%s\n", xui));
        sb.append(String.format("filename:%s\n", filename));
        sb.append(String.format("documentSelectorArray:%s\n", Arrays.deepToString(documentSelectorArray)));
        sb.append(String.format("nodeSelectorArray:%s\n", Arrays.deepToString(nodeSelectorArray)));
        return sb.toString();
    }

    public static void main(String args[]) {
        XcapParser parser = new XcapParser();
        String xcapUri = "/public-group/users/sip:my_group@feinno.com/index.xml/~~/public-group/list/entry[@uri='sip:bob@feinno.com']";
        parser.parse(xcapUri);
        System.out.println(parser);

    }
}
